<template>
	<view class="order-list">

		<view class="nav">
			<view  @tap="changeOrderStatus('')" v-bind:class="{'active':!order_status}" class="item ">全部</view>
			<view  @tap="changeOrderStatus('pending')" v-bind:class="{'active':order_status=='pending'}" class="item ">待付款</view>
			<view  @tap="changeOrderStatus('processing')" v-bind:class="{'active':order_status=='processing'}" class="item ">待发货</view>
			<view  @tap="changeOrderStatus('shipping')" v-bind:class="{'active':order_status=='shipping'}" class="item ">待收货</view>
			<view  @tap="changeOrderStatus('apprising')" v-bind:class="{'active':order_status=='apprising'}" class="item ">待评价</view>
			<view  @tap="changeOrderStatus('complete')" v-bind:class="{'active':order_status=='complete'}" class="item ">完成</view>
		</view>

		<view class="order-list">

			<view class="order-item">

			</view>
		</view>

		<!--<view class="header">
                    <view class="status-text">等待买家付款</view>
                    <view class="dianpu">店铺：<text>虾米商城</text></view>
                    <view class="order-no">订单编号：<text>E20200526143023451</text></view>
                </view>-->

		<view class="list">
			<block v-for="order,index in order_list">
				<view class="item">
					<navigator v-bind:url="'/sm_shop/pages/order/single?order_id=' + order.id">
						<view class="header">
							<view v-show="order.is_payed!=1" class="status-text">等待买家付款</view>
							<view class="dianpu">店铺：<text>{{shop_name}}</text></view>
							<view class="order-no">
								<text>订单编号：{{order.order_no}}</text>
								<text>{{order.create_time}}</text>
							</view>
						</view>
						<block v-for="item,index in order.goods">
							<view class="product">
								<view class="img">
									<image mode="widthFix" v-bind:src="item.image"></image>
								</view>
								<view class="info">
									<view class="name">{{item.goods_name}}</view>
									<view class="product-option">{{item.goods_option}}</view>
									<view class="product-option">{{item.product_option}}</view>
									<view class="price-number">
										<view class="price">￥{{item.goods_price}}</view>
										<view class="number">x <text>{{item.goods_num}}</text></view>
									</view>
								</view>
								<view class="clear"></view>
							</view>

						</block>

					</navigator>

					<view class="total">
						合计：<text>￥{{order.total}}</text>
					</view>

					<view v-show="order.is_payed!=1" class="panel">
						<view v-show="order.is_payed!=1" @tap="orderCancel(order)" class="btn cancel">取消</view>
						<view v-show="order.is_payed!=1" @tap="orderPay(order)" class="btn pay">付款</view>
						<view v-show="order.status=='shipping'" @tap="orderReceive(order)" class="btn pay">确认收货</view>
					</view>

				</view>
			</block>


		</view>



	</view>
</template>

<script>
	var appInstance = getApp();
	export default {
		data() {
			return {
				shop_name: '',
				order_status: '',
				order_list: []
			}
		},
		onLoad: function(options) {

			this.shop_name = appInstance.globalData.shop_name;

			var t = this;
			if (options.status) {
				this.order_status = options.status
				t.get_order_list();


			} else {

				t.get_order_list();

			}

		},		
		methods: {

			get_order_list: function(resolve) {

				var t = this;
				var url = '&r=checkout.order.get_list';
				var data = {
					status: this.order_status
				};

				appInstance.globalData.request(url, data, "GET", res => {

					if (res.data.list) {

						t.order_list = res.data.list;
					}
					if (typeof resolve == 'function') {
						resolve(res.data);
					}
				});

			},

			changeOrderStatus: function( order_status ) {

				// var order_status = e.currentTarget.dataset.status;

				this.order_status = order_status;

				//todo 获取 该状态下的订单列表
				this.get_order_list();



			},


			orderPay: function( order ) {

				//todo 调启微信支付
				var url = '&r=checkout.xpay.index_xcx';

				// var order = e.currentTarget.dataset.order;
				var data = {
					order_id: order.id
				};

				var t = this;
				appInstance.globalData.request(url, data, 'GET', res => {

					if (res.data.nonceStr) {
						uni.requestPayment({
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							signType: res.data.signType,
							paySign: res.data.paySign,
							success(res) {
								// t.order_list();

								t.pay_notify(order);
							},
							fail(res) {

							}
						});

					}else if( res.data.status ){
						uni.showToast({
						  title: res.data.description,
						  icon: 'none',
						  duration: 1200
						});
					}


				});

			},

			/**
			 * 订单接收
			 */
			orderReceive:function( order ){
				var t = this;
				var url = '&r=checkout.order.receive';
				var data = {
					'order_id': order.id
				};
				appInstance.globalData.request(url, data, 'POST', function() {
				
					t.get_order_list();
				
				});
			},
			
			pay_notify: function(order) {

				var t = this;
				var url = '&r=checkout.xpay.pay_notify';
				var data = {
					'order_id': order.id
				};
				appInstance.globalData.request(url, data, 'POST', function() {

					t.get_order_list();

				});

			},


			orderCancel: function( order ) {

				var t = this;
				var url = '&r=checkout.order.cancel';
				var data = {
					order_id: order.id,
				};

				appInstance.globalData.request(url, data, "GET", res => {

					t.get_order_list();

				});

			},

		},
		onPullDownRefresh: function() {

			var p = new Promise(this.get_order_list);
			p.then(function(result) {
				uni.stopPullDownRefresh();
			});
		},
	}
</script>

<style lang="less">
	@import './list.less';
</style>
